package momo.multitree.simulation;

import java.io.FileReader;

import momo.multitree.structure.*;
import junit.framework.TestCase;

public class SimulatedAnnealingTest extends TestCase
{	
	public void testGetBestTree()
	{
		try
		{
			int maxIteration = 100;
			//int maxTrial = 11200;
			int maxTrial = 100;
			//double temperature = 1d;
			double temperature = 1d;
			double factor = 0.95d;
			
			String filename = "10_node_symmetric_dataset_55.txt";
			Graph graph = new Graph(new FileReader("dataset/"+filename));
			SimulatedAnnealing app = new SimulatedAnnealing(graph, 
															maxIteration, 
															maxTrial, 
															temperature, 
															factor);
			
			Tree bestTree = app.optimizeTree();
			
			double lat = bestTree.compWeightedLatency();
			double cost = bestTree.compCost(false);
			double stab = bestTree.compStability();
			
			double bestLat = 167.2d;
			double worstLat = 769.7d;
			double bestCost = 151.5d;
			double worstCost = 863d;
			double bestStab =  0.923247405d;
			double worstStab = 4.780583679d;
			
			 
			double latT = ( lat - bestLat ) / ( worstLat - bestLat );
			if ( Double.isNaN(latT) || Double.isInfinite(latT) ) latT = 0;
			double costT = ( cost - bestCost ) / ( worstCost - bestCost );
			if ( Double.isNaN(costT) || Double.isInfinite(costT) ) costT = 0;
			double stabT = ( stab - bestStab ) / ( worstStab - bestStab );
			if ( Double.isNaN(stabT) || Double.isInfinite(stabT) ) stabT = 0;
			
			
			double MC_DistFromIdeaPt = 0.215094736;
			double SA_DistFromIdeaPt = Math.sqrt( Math.pow(latT, 2) + Math.pow(costT, 2) + Math.pow(stabT, 2) );
			
			double diff = SA_DistFromIdeaPt - MC_DistFromIdeaPt;
			double error = diff / MC_DistFromIdeaPt;
			if ( Double.isNaN(error) || Double.isInfinite(error) ) error = 0;
			
			System.out.println("Lat: " + lat);
			System.out.println("Cost: " + cost);
			System.out.println("Stab: " + stab);
			System.out.println("SA: " + SA_DistFromIdeaPt);
			System.out.println("MC: " + MC_DistFromIdeaPt);
			System.out.println("diff: " + diff);
			System.out.println("error: " + error);
			
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}